package com.android.support.lib.universal.view.web;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.base.BaseActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.TextView;

/**
 * 引用样式
 * lib_webview_progress_bar_circle.xml（圆形进度）lib_webview_loading_ios.png(圆形进度图片
 * )，lib_webview_progress_bar_horizontal.xml（水平进度） 
 * 注册： <activity
 * android:name="com.android.support.lib.universal.view.web.WebActivity"
 * android:theme="@android:style/Theme.NoTitleBar"
 * android:configChanges="orientation|keyboardHidden" > </activity> 
 * 权限：
 * <uses-permission android:name="android.permission.INTERNET"/>
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 * 
 * 
 * @author 彭治铭
 *
 */
public class WebActivity extends BaseActivity implements OnClickListener {
	private Uwebview uwebview;;
	private CustomViewCallback customViewCallback;

	/**
	 * url访问异常处理
	 */
	public void urlerror() {
		/**
		 * 载入本地assets文件夹下面的错误提示页面404.html
		 */
		// uwebview.owebview.loadUrl("file:///android_asset/404.html");
		/**
		 * 用javascript隐藏系统定义的404页面信息
		 */
		String data = "Page NO FOUND!";
		uwebview.owebview.loadUrl("javascript:document.body.innerHTML=\""
				+ data + "\"");
	}

	String url;
	Boolean boolCircle;
	Boolean boolZoom;
	Boolean boolProbar;
	int height;

	String title = "详情";// 记录当前标题栏

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lib_activity_webview);
		Bundle bundle = this.getIntent().getExtras();
		if (bundle == null) {
			bundle = new Bundle();
		}
		url = bundle.getString("url", "http://www.uc123.com/");// 访问的url,后面的是缺省值。
		boolCircle = bundle.getBoolean("boolCircle", false);
		boolZoom = bundle.getBoolean("boolZoom", false);
		boolProbar = bundle.getBoolean("boolProbar", false);
		height = bundle.getInt("height", 3);
		if (bundle.get("title") != null) {
			title = bundle.getString("title", "详情");
			// 标题栏中隐藏的文本框
			// TextView fragment_hide = (TextView)
			// findViewById(R.id.fragment_hide);
			// fragment_hide.setText("发送");
			// fragment_hide.setAlpha(0.4f);
			// fragment_hide.setEnabled(false);
			// fragment_hide.setVisibility(0);
		}
		// 标题栏
		TextView fragment_title = (TextView) findViewById(R.id.fragment_title);
		fragment_title.setText(title);

		/**
		 * 
		 * @param context
		 *            Activity上下文
		 * @param boolCircle
		 *            是否为圆形进度条(false 为 水平进度条)
		 * @param boolZoom
		 *            网页是否支持缩放(包括双击缩放)
		 * @param boolProbar
		 *            是否显示webview的滑动条
		 * @param height
		 *            水平进度条的高度
		 * 
		 */
		uwebview = new Uwebview(this, boolCircle, boolZoom, boolProbar, height);
		uwebview.owebview.setWebViewClient(new WebViewClient() {
			/**
			 * 重写此方法，浏览器内部跳转,Activity之间的调整，解决webView的返回刷新
			 */
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Count.count++;
				if (Count.count > 1) {
					Count.activitiesList.add(WebActivity.this);
				}
				Intent intent = new Intent();
				intent.setClass(WebActivity.this, WebActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("url", url);
				bundle.putString("title", title);//标题
				bundle.putBoolean("boolCircle", boolCircle);
				bundle.putBoolean("boolZoom", boolZoom);
				bundle.putBoolean("boolProbar", boolProbar);
				bundle.putInt("height", height);
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(R.anim.lib_webview_hide,
						R.anim.lib_webview_hide);// 利用动画隐藏Acitity跳转效果
				// 去除多次重定向的无用页面(即快速跳转的页面或重复的页面，我认为是无用的页面即处理掉了)
				if ((System.currentTimeMillis() - Count.lasttTime) < 3500
						|| url.equals(Count.currentUrl)) {

					WebActivity.this.finish();
				}
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				if (Count.count > 1) {
					Count.exit();
				} else {
					Count.count = 0;
				}
			}

			/**
			 * 重写404页面信息
			 */
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				urlerror();
			}
		});
		Count.lasttTime = System.currentTimeMillis();// 记录当前Activity加载的时间
		Count.currentUrl = url;// 记录当前Activity的url
		uwebview.owebview.loadUrl(url);
	}

	/**
	 * 捕捉返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			uwebview.owebview.mWebView.reload();// 重新加载是为了关闭视频,不会调用shouldOverrideUrlLoading()方法。
			this.finish();

			return true;// 返回键
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if (id == R.id.txt_return || id == R.id.fragment_return) {// 返回
			uwebview.owebview.mWebView.reload();// 重新加载是为了关闭视频,不会调用shouldOverrideUrlLoading()方法。
			this.finish();
		}
	}

	@Override
	protected void initUI() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void listenner() {
		// TODO Auto-generated method stub

	}

}